//
//  ThumbnailsController.m
//  PXListView
//
//  Created by DuyLK on 6/10/14.
//
//

#import "ThumbnailsController.h"
#import "MyListViewCell.h"


#pragma mark Constants

#define LISTVIEW_CELL_IDENTIFIER		@"MyListViewCell"
@implementation ThumbnailsController

-(id) initWithDocument:(fz_document *)doc
{
    self = [super init];
    if (self) {
        [listView setCellSpacing: 2.0f];
        //[listView setAllowsEmptySelection: YES];
        [listView setAllowsMultipleSelection: YES];
        [listView registerForDraggedTypes: [NSArray arrayWithObjects: NSStringPboardType, nil]];
        
        _listItems = [[NSMutableArray alloc] init];
        document = doc;
        _totalItem = fz_count_pages(doc);
        // Create a bunch of rows as a test:
        for( NSInteger i = 0; i < _totalItem ; i++ )
        {
            NSString *title = [[NSString alloc] initWithFormat: @"%ld", i +1]; // We're in a tight loop
            [_listItems addObject: title];
            [title release];
        }
    }
    return  self;
}

-(id) initWithDocument:(fz_document *)doc :(int)widthPage :(int)heightPage
{
    self = [super init];
    if (self) {
        _withPage = widthPage;
        _heightPage = heightPage;
        [listView setCellSpacing: 2.0f];
        //[listView setAllowsEmptySelection: YES];
        [listView setAllowsMultipleSelection: YES];
        [listView registerForDraggedTypes: [NSArray arrayWithObjects: NSStringPboardType, nil]];
        
        _listItems = [[NSMutableArray alloc] init];
        document = doc;
        _totalItem = fz_count_pages(doc);
        // Create a bunch of rows as a test:
        for( NSInteger i = 0; i < _totalItem ; i++ )
        {
            NSString *title = [[NSString alloc] initWithFormat: @"%ld", i +1]; // We're in a tight loop
            [_listItems addObject: title];
            [title release];
        }
    }
    return  self;
}

- (void)dealloc
{
	[_listItems release];
	[super dealloc];
}

#pragma mark -
#pragma mark List View Delegate Methods

- (NSUInteger)numberOfRowsInListView: (PXListView*)aListView
{
#pragma unused(aListView)
	return [_listItems count];
}

- (PXListViewCell*)listView:(PXListView*)aListView cellForRow:(NSUInteger)row
{
	MyListViewCell *cell = (MyListViewCell*)[aListView dequeueCellWithReusableIdentifier:LISTVIEW_CELL_IDENTIFIER];
	
	if(!cell) {
		cell = [[[MyListViewCell alloc] initWithReusableIdentifier:LISTVIEW_CELL_IDENTIFIER] autorelease];
	}
    cell.delegate = self;
	// Set up the new cell:
	[cell setTitle: [_listItems objectAtIndex: row]];
    [cell setDoc:document];
    return cell;
}

- (CGFloat)listView:(PXListView*)aListView heightOfRow:(NSUInteger)row
{
    
    
	return _heightPage;
}
-(void) showHideThumbnails:(BOOL)isHiden
{
    if(isHiden)
    {
        //neu muon an
        if ([listView isHidden]) {
            [listView setHidden:NO];
        }
    }else{
        if (![listView isHidden]) {
            [listView setHidden:YES];
        }
    }
}
-(void)selectItem:(int)numberPage
{
    [_delegate selectPage:(numberPage)];
}
@end
